package com.flyco.modules.pms.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flyco.modules.pms.entity.PmsSpuProduct;
import com.flyco.modules.pms.model.PmsSpuModel;
import com.flyco.modules.pms.service.IPmsSpuProductService;
import org.apache.shiro.SecurityUtils;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.pms.entity.PmsProduct;
import com.flyco.modules.pms.entity.PmsSpu;
import com.flyco.modules.pms.service.IPmsSpuService;
import com.flyco.modules.pms.service.IPmsProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: SPU管理
 * @Author: flyco
 * @Date:   2020-03-09
 * @Version: V1.0
 */
@RestController
@RequestMapping("/f/pms/pmsSpu")
@Slf4j
public class PmsSpuController {
	@Autowired
	private IPmsSpuService pmsSpuService;
	@Autowired
	private IPmsProductService pmsProductService;
	@Autowired
	private IPmsSpuProductService spuProductService;
	
	/**
	 * 分页列表查询
	 *
	 * @param pmsSpu
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PmsSpu pmsSpu,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PmsSpu> queryWrapper = QueryGenerator.initQueryWrapper(pmsSpu, req.getParameterMap());
		Page<PmsSpu> page = new Page<PmsSpu>(pageNo, pageSize);
		IPage<PmsSpu> pageList = pmsSpuService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param pmsSpu
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PmsSpu pmsSpu) {
		pmsSpuService.save(pmsSpu);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param pmsSpu
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PmsSpu pmsSpu) {
		pmsSpuService.updateById(pmsSpu);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	@Transactional
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		pmsSpuService.removeById(id);
		spuProductService.delMain(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	@Transactional
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
	    this.pmsSpuService.removeByIds(Arrays.asList(ids.split(",")));
		spuProductService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PmsSpu pmsSpu = pmsSpuService.getById(id);
		return Result.ok(pmsSpu);
	}
	
	//===========================以下是子表信息操作相关API====================================
	
	/**
	 * 通过主表id查询商品管理
	 *
  	 * @param pmsProduct
	 * @return
	 */
	@GetMapping(value = "/listPmsProductByMainId")
	public Result<?> listPmsProductByMainId(PmsProduct pmsProduct,
                								  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                								  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                								  HttpServletRequest req) {
        if(pmsProduct.getSpu()!=null) {
            QueryWrapper<PmsProduct> queryWrapper = QueryGenerator.initQueryWrapper(pmsProduct, req.getParameterMap());
            Page<PmsProduct> page = new Page<PmsProduct>(pageNo, pageSize);
            IPage<PmsProduct> pageList =  pmsProductService.page(page, queryWrapper);
            return Result.ok(pageList);
        }else{
            return Result.error("请选择SPU");
        }
    }
	
	/**
     * 添加商品管理
     *
     * @param model
     * @return
     */
    @PostMapping(value = "/addPmsProduct")
    public Result<?> addPmsProduct(@RequestBody PmsSpuModel model) {
		if(null != model && null != model.getProductIdList() && null != model.getSpuId()) {
			List<PmsSpuProduct> products = new ArrayList<>();
			for (Long productId : model.getProductIdList()){
				PmsSpuProduct product = new PmsSpuProduct();
				product.setId(productId);
				product.setSpu(model.getSpuId());
				products.add(product);
			}
			spuProductService.updateBatchById(products);
			return Result.ok("添加商品成功！");
		}
		return Result.error("添加商品失败！");
    }
    
    /**
     * 通过id删除商品管理
     *
     * @param id
     * @return
     */
	@DeleteMapping(value = "/deletePmsProduct")
	public Result<?> deletePmsProduct(@RequestParam(name = "id", required = true) Long id) {
		PmsSpuProduct product = new PmsSpuProduct();
		product.setId(id);
		product.setSpu(null);
		spuProductService.updateById(product);
		return Result.ok("删除商品成功！");
	}
	
	/**
     * 批量删除商品管理
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatchPmsProduct")
    public Result<?> deleteBatchPmsProduct(@RequestParam(name = "ids", required = true) String ids) {
        if (ids == null || "".equals(ids.trim())) {
            return Result.error("参数不识别！");
        }
        List<String> strs = Arrays.asList(ids.split(","));
		List<PmsSpuProduct> products = new ArrayList<>();
		for (String productId : strs){
			PmsSpuProduct product = new PmsSpuProduct();
			product.setId(Long.valueOf(productId));
			product.setSpu(null);
			products.add(product);
		}
		spuProductService.updateBatchById(products);
       	return Result.ok("批量删除成功！");

    }
}
